package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.ProcessService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.ProcessForm;
import com.youlai.boot.factory.model.query.ProcessQuery;
import com.youlai.boot.factory.model.vo.ProcessVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 工序基础信息前端控制层
 *
 * @author youlaitech
 * @since 2025-10-01 05:02
 */
@Tag(name = "工序基础信息接口")
@RestController
@RequestMapping("/api/v1/process")
@RequiredArgsConstructor
public class ProcessController  {

    private final ProcessService processService;

    @Operation(summary = "工序基础信息分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:process:query')")
    public PageResult<ProcessVO> getProcessPage(ProcessQuery queryParams ) {
        IPage<ProcessVO> result = processService.getProcessPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增工序基础信息")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:process:add')")
    public Result<Void> saveProcess(@RequestBody @Valid ProcessForm formData ) {
        boolean result = processService.saveProcess(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取工序基础信息表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:process:edit')")
    public Result<ProcessForm> getProcessForm(
        @Parameter(description = "工序基础信息ID") @PathVariable Long id
    ) {
        ProcessForm formData = processService.getProcessFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改工序基础信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:process:edit')")
    public Result<Void> updateProcess(
            @Parameter(description = "工序基础信息ID") @PathVariable Long id,
            @RequestBody @Validated ProcessForm formData
    ) {
        boolean result = processService.updateProcess(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除工序基础信息")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:process:delete')")
    public Result<Void> deleteProcesss(
        @Parameter(description = "工序基础信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = processService.deleteProcesss(ids);
        return Result.judge(result);
    }
}
